<template>
	<view class="main">
		<view class="list">
			<input type="text" v-model="form.username" placeholder="请输入联系人姓名"  placeholder-style="font-size:28rpx;color:#CCCCCC;"  />
		</view>
		<view class="list">
			<input type="text" v-model="form.phone" placeholder="请输入联系人电话"  placeholder-style="font-size:28rpx;color:#CCCCCC;" />
		</view>
		
		<view class="bottom">
			<!-- 新增 -->
			<template v-if="!isEdit"><btn  @click="save()" title="保存"></btn></template>
			<!-- 编辑 -->
			<template v-else>
				<!-- 保存按钮 -->
				<btn  @click="save()" title="保存" style="margin-bottom: 48rpx;"></btn>
			
				<btn  type="info" title="删除" @click="del"></btn>
			</template>
		</view>
	</view>
</template>

<script>
	import btn from '@/components/common/btn/btn.vue';
	import { validateUsername, validatePhone } from '@/common/js/validate.js';
	import {addUrgencyLinkman} from '../../../apis/security/index.js'
	import {debounce} from '@/common/js/throttle.js'
	export default {
		components: {
			btn
		},
		data() {
			return {
				form:{
					
				},
				isEdit:false
				
			}
		},
		onLoad(option) {
			if(option.type){
				this.isEdit=option.type=='0'?false:true
				console.log(this.isEdit);
			}
		},
		methods: {
			// 保存
			save: debounce(async  function() {
				// 验证联系人姓名
				if(validateUsername(this.form.username) && validatePhone(this.form.phone,86)){
					console.log('保存');
					if(this.$store.state.userInfo.mobile==this.form.phone){
						plus.nativeUI.toast('不能添加自己为紧急联系人~')
						// uni.showToast({
						// 	title:'不能添加自己为紧急联系人~',
						// 	icon:'none'
						// })
						return
					}
					let data={
						mobile:this.form.phone,
						name:this.form.username,
						nurseId:this.$store.state.userInfo.id
					}
					let res=await addUrgencyLinkman(data)
					if(res.success){
						plus.nativeUI.toast('添加成功~')
						// uni.showToast({
						// 	title:'添加成功~',
						// 	image:'../../static/img/success.png'
						// })
					}
					
				}
			},2000,true),
			del(){
				uni.showModal({
					title:'确认删除该紧急联系人吗？'
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.list{
		padding: 32rpx 0rpx;
		margin: 0 26rpx;
		border-bottom: solid 1rpx #E5E5E5;
	}
	.bottom{
		position: fixed;
		bottom: 68rpx;
		margin-left: 26rpx;
	}

</style>
